#include "spi_flash_fatfs_driver.h"

static const char *TAG = "spi_flash_fatfs_driver";

esp_err_t spi_flash_fatfs_init(void)
{
    const char *base_path = SPI_FLASH_MOUNT_POINT;

    esp_vfs_fat_mount_config_t mount_config = {
        .max_files = 5,                   
        .format_if_mount_failed = true,   
        .allocation_unit_size = 16 * 1024 
    };

    wl_handle_t wl_handle = WL_INVALID_HANDLE;

    esp_err_t err = esp_vfs_fat_spiflash_mount_rw_wl(base_path, "storage", &mount_config, &wl_handle);
    if (err != ESP_OK)
    {
        ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
        return ESP_FAIL;
    }
    ESP_LOGI(TAG, "FATFS mounted on %s", base_path);
    return ESP_OK;
}